package com.farm.template.utils;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class FarmJsonUtils {
	public static String toJson(Map<String, Object> map) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			String jsonStr = mapper.writeValueAsString(map);
			return jsonStr;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static Map<String, Object> jsonStrToObjects(String json) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			// 将 JSON 字符串转换为 Map<String, Object>
			Map<String, Object> map = mapper.readValue(json,
					TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class));
			return map;
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		}
	}
}
